﻿
using Furion.Core;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using IdentityModel;
using IdentityServer4.Extensions;
using IdentityServer4.Models;
using IdentityServer4.Services;
using Microsoft.EntityFrameworkCore;
using StackExchange.Profiling.Internal;
using System.Security.Claims;

namespace IdentityServer4CenterPassword
{
    public class ProfileServices : IProfileService
    {
        
        private readonly IRepository<User> _userRepository;

        public ProfileServices(IRepository<User> userRepository)
        {
            _userRepository = userRepository;
        }



        /// <summary>
        /// http://localhost:5002/connect/userinfo
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            //var userId = context.Subject.Claims.FirstOrDefault(c => c.Type == JwtClaimTypes.Id);
            //if (userId != null)
            //{
            //    var userInfo = await _userRepository.Include(a => a.Roles).FirstOrDefaultAsync(a => a.Id == Convert.ToInt32(userId.Value));
            //    if (userInfo != null)
            //    {
            //        var Ids = userInfo.Roles.Select(a => a.Id).ToList();
            //        var roles = userInfo.Roles.Adapt<ICollection<RoleOutput>>();

            //        var permissionIds = await _rolePermissionRepository.DetachedEntities.Where(a => Ids.Contains(a.RoleId)).Select(a => a.PermissionId).Distinct().ToListAsync();
            //        var permissions = _permissionRepository.DetachedEntities.Where(a => permissionIds.Contains(a.Id)).ToList().Adapt<List<PermissionOutInput>>();

            //        var claims = new List<Claim>
            //        {
            //            new Claim(JwtClaimTypes.Id, userInfo.Id.ToString()),
            //            new Claim(JwtClaimTypes.NickName,userInfo.NickName),
            //            new Claim(JwtClaimTypes.PreferredUserName, userInfo.UserName),
            //            new Claim(JwtClaimTypes.Role, roles.ToJson()),
            //            new Claim(JwtClaimTypes.ZoneInfo, permissions.ToJson())
            //        };

            //        context.IssuedClaims = claims;
            //    }
            //    else
            //    {
            //        context.IssuedClaims = null;
            //    }
            //}
            //else
            //{
            //    context.IssuedClaims = null;
            //}

            var claims = context.Subject.Claims.ToList();
            context.IssuedClaims = claims.ToList();

            return Task.CompletedTask;
        }

        public async Task IsActiveAsync(IsActiveContext context)
        {
            var userId = context.Subject.Claims.FirstOrDefault(c => c.Type == ClaimConst.CLAINM_USERID);
            if (userId != null)
            {
                int id=Convert.ToInt32(userId.Value);
                context.IsActive = await _userRepository.DetachedEntities.AnyAsync(a => a.Id == id);
            }
            else
            {
                context.IsActive = false;
            }
        }


    }
}
